package com.dhcc.ws.controller;
import javax.wsdl.WSDLException;

import com.dhcc.ws.bean.ParameterInfo;
import com.dhcc.ws.util.WAWsdlUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wangleai on 2018/3/1.
 * 第一步，使用wsdl4j解析wsdl，wsdl文档结构推荐参考http://blog.csdn.net/wudouguerwxx/article/details/2036821
 * 第二步，使用DOM解析XML Schema，获取元素名称和类型
 */
public class Test {
    public static void main(String[] args) throws WSDLException {
//        String wsdluri = "http://ws.webxml.com.cn/webservices/qqOnlineWebService.asmx?wsdl";
//        String wsdluri = "http://192.168.5.60:8090/rarp/ws/arpToThirdWService?wsdl";
//        String wsdluri = "http://www.webxml.com.cn/webservices/weatherwebservice.asmx?wsdl";
        String wsdluri = "http://127.0.0.1:8081/rarp/ws/appWService?wsdl";
        String targetNamespace =  WAWsdlUtil.getTargetNamespace(wsdluri);
        System.out.println(targetNamespace);
        List<String> operations = new ArrayList<>();
        WAWsdlUtil.getOperationList(wsdluri, operations);
        
        for (String operationName : operations) {
            System.out.println("-----------------方法----------------"+operationName);
//            System.out.println(operationName);
            List<ParameterInfo> parameterInfos = WAWsdlUtil.getMethodParams(wsdluri, operationName);
            printParams(parameterInfos, "");
            List<ParameterInfo> returnInfos = WAWsdlUtil.getMethodReturn(wsdluri, operationName);
            printReturnParams(returnInfos, "");
        }
    }

    private static void printParams(List<ParameterInfo> parameterInfos, String parentName) {
        if (parameterInfos != null) {
            for (ParameterInfo parameterInfo : parameterInfos) {
                System.out.println("parentname : " + parentName + " ; name : " + parameterInfo.getName() + " ; type :" +
                        " " + parameterInfo.getType() + " ;" +
                        " childtype : " + parameterInfo.getChildType());
                printParams(parameterInfo.getChildren(), parameterInfo.getName());
            }
        }
    }
    private static void printReturnParams(List<ParameterInfo> parameterInfos, String parentName) {
    	if (parameterInfos != null) {
    		for (ParameterInfo parameterInfo : parameterInfos) {
    			System.out.println("parentname : " + parentName + " ; name : " + parameterInfo.getName() + " ; type :" +
    					" " + parameterInfo.getType() + " ;" +
    					" childtype : " + parameterInfo.getChildType());
    			printParams(parameterInfo.getChildren(), parameterInfo.getName());
    		}
    	}
    }
}
